<?php

namespace App\VO\ConvertibleCardUseRecord;

use App\BO\ConvertibleCard\Constant as ConvertibleCardConstant;
use App\Entities\ConvertibleCardEntity;
use App\Entities\ConvertibleCardUseRecordEntity;
use App\Entities\OrderEntity;
use App\VO\Interfaces\IVOListItem;
use Illuminate\Support\Carbon;

class VOListItem implements IVOListItem
{
    // use CommonTrait;

    public $id = 0;
    // public $title = '';
    // public $price = 0;//分
    // public $endSaleDate = '';
    // public $bannerImage = '';
    // public $purchaseLimit = 0;
    public $actionStr = '';
    public $createTime = '';
    public $recordName = '';

    public function __construct(ConvertibleCardUseRecordEntity $recordEntity)
    {
        $this->id = $recordEntity->getId();
        $this->actionStr = $this->action($recordEntity);
        $this->createTime = $recordEntity->getCreatedAt();
        if ($this->createTime instanceof Carbon) {
            $this->createTime = $this->createTime->toDateTimeString();
        }
        $this->recordName = $this->recordName($recordEntity);
    }

    private function action(ConvertibleCardUseRecordEntity $recordEntity)
    {
        $type = $recordEntity->getType();
        $value = $recordEntity->getValue();
        $str = '';
        switch ($type) {
            case 1:
            case 3:
                $str .= '+';
            break;
            case 2:
                $str .= '-';
            break;
        }
        $str .= $value . '次';
        return $str;
    }

    private function recordName(ConvertibleCardUseRecordEntity $recordEntity)
    {
        $str = '';
        $type = $recordEntity->getType();
        $cardId = $recordEntity->getConvertibleCardId();
        $orderId = $recordEntity->getOrderId();
        $cardEntity = ConvertibleCardEntity::find($cardId);
        $orderEntity = OrderEntity::find($orderId);

        switch ($type) {
            case 1:
                $str = ConvertibleCardConstant::RECORD_TYPES_NAMES[$type] ?? '';
                if ($cardEntity) {
                    $str .= $cardEntity->getTitle();
                }
            break;
            case 2:
            case 3:
                if ($orderEntity) {
                    $skuTitle = $orderEntity->getSkuTitle();
                    $str .=  $skuTitle;
                }
            if ($type == 3) {
                $str .= '退单';
            }
            break;
            // case 3:
                // $str .= '退回';
            // break;
        }
        return $str;
    }
}
